package com.examsystem.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 系统管理控制器类
 * @author An-Il
 *
 */

import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.entity.exame_subjecttype;
import com.examsystem.service.SubjectTypeService;
/**
 * 系统管理
 * @author An-Il
 *
 */
@RestController
@RequestMapping("/xtgl")
public class SubjectTypeController {

	@Autowired
	private SubjectTypeService sTypeService;

	/**
	 * 根据id删除记录(支持批量删除)
	 * 批量：1-2-3
	 * 单个：1
	 * @param subjecttype
	 * @return
	 */
	@PostMapping("/deleteByKey")
	public ResultJson deleteByKey(String ids) {
		ResultJson result = null;
		if(StringUtils.isBlank(ids)) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
		}else {
			//批量删除
			List<Integer> del_ids = new ArrayList<>();
			if(ids.contains("-")) {
				String[] str_ids = ids.split("-");
				//组装id的集合
				for (String string : str_ids) {
					del_ids.add(Integer.parseInt(string));
				}
			}else {
				del_ids.add(Integer.parseInt(ids));
			}
			
			result = sTypeService.deleteByKey(del_ids);			
		}
		return result;
	}

	/**
	 * 根据id主键修改记录
	 * 
	 * @param subjecttype
	 * @return
	 */
	@PostMapping("/updateByKey")
	public ResultJson updateByKey(exame_subjecttype subjecttype) {
		ResultJson result = null;
		result = sTypeService.updateByKey(subjecttype);
		return result;
	}

	/**
	 * 模糊查询
	 * 
	 * @param subjecttype
	 * @return
	 */
	@PostMapping("/selectByExample")
	public ResultJson selectByExamp(exame_subjecttype subjecttype, @RequestParam(defaultValue = "DESC") String Sort,
			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
		ResultJson result = null;
		result = sTypeService.selectByExamp(subjecttype, Sort, page, pageSize);
		return result;
	}

	/**
	 * 根据当前页及每页显示数量查询
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/selectSubjectList")
	public ResultJson selectSubjectList(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		ResultJson result = null;
		result = sTypeService.selectSubjectTypeList(page, pageSize);
		return result;
	}
	
	@PostMapping("/insertSubject")
	public ResultJson insertSubject(exame_subjecttype subjecttype) {
		ResultJson result=null;
		//信息判断
		if(StringUtils.isBlank(subjecttype.getStname())||StringUtils.isBlank(subjecttype.getStname())) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
		}else {
			result=sTypeService.insertInfo(subjecttype);
		}
		return result;
	}

}
